// testing of the dominion.c scoreFor() function

#include <stdio.h>
#include <stdlib.h>
#include "dominion.h"
#include "rngs.h"

#define MAX_CARD treasure_map

typedef struct gameState Gamestate;

//prints the results of the testing
void print_results(int a, int b){
	double results = ((double)a) / b;
	printf("---------------------\n");
	printf("scoreFor() -- %.f%% of test cases passed (%d/%d)\n", (results*100), a, b);
}

//sets all the values in the game struct to 0
void init_gamestate(Gamestate *g){
	int i;
	for(i = 0; i < sizeof(Gamestate); i++){
		((char *)g)[i] = 0;
	}
}

int main(int argc, char **argv) {
	int i, j;
	int player, handCount, discardCount, deckCount, totalCount, card;
	int values[MAX_CARD+1];
	int quantity[6];
	int randomSeed, test_count;
	int score, result;
	int good_test, bad_test;
	double test_results;
	Gamestate g;
	
	if(argc != 3){
		printf("Usage: unittest4 [seed] [number of tests]\n");
		return(0);
	}
	
	//initialize vars
	randomSeed = atoi(argv[1]);
	test_count = atoi(argv[2]);
	good_test = bad_test = 0;
	for(i = 0; i <= MAX_CARD; i++){
		values[i] = 0;
	}
	values[curse] = -1;
	values[estate] = 1;
	values[duchy] = 3;
	values[province] = 6;
	values[great_hall] = 1;
		
	if(test_count < 0){
		printf("Please provide a positive integer for [number of tests]\n");
		return(0);
	}
	
	// set up random number
	SelectStream(1);
	PutSeed((long)randomSeed);
	
	for(i = 0; i < test_count; i++){
		init_gamestate(&g);
		score = 0;
		//create random inputs
		player = (int)(Random()*3)+1; //pick a player number
		handCount = (int)(Random()*(MAX_DECK/3)); //size of hand
		deckCount = (int)(Random()*((MAX_DECK/3)-1))+1; //size of deck > 0
		discardCount = (int)(Random()*(MAX_DECK/3)); //size of discard
		totalCount = handCount + deckCount + discardCount;
		
		//create the hand
		for(j = 0; j < handCount; j++){
			card = (int)(Random()*MAX_CARD); //get a random card 
			g.hand[player][j] = card; //put the card in the hand
			score += values[card]; //add the card's value to our score
			if(card == gardens){
				score += (int)(totalCount/10); //handle garden card
			}
		}
		
		//create the deck
		for(j = 0; j < deckCount; j++){
			card = (int)(Random()*MAX_CARD); //get a random card 
			g.deck[player][j] = card; //put the card in the deck
			score += values[card]; //add the card's value to our score
			if(card == gardens) score += (int)(totalCount/10); //handle garden card
		}
		
		//create the discard pile
		for(j = 0; j < discardCount; j++){
			card = (int)(Random()*MAX_CARD); //get a random card
			g.discard[player][j] = card; //put the card in the discard pile
			score += values[card]; //add the card's value to our score
			if(card == gardens) score += (int)(totalCount/10); //handle garden card
		}
		
		//assign inputs
		g.handCount[player] = handCount;
		g.deckCount[player] = deckCount;
		g.discardCount[player] = discardCount;
		
		//call the function
		result = scoreFor(player, &g);
		if(result != score){
			printf("Error: scores don't match [%d != %d]\n",score, result);
			bad_test++;
		}
		else good_test++;
	}

	print_results(good_test, test_count);
	
	return 0;
}
